package cn.jsj.hzu.qm.facecloud.ciuser.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import cn.jsj.hzu.qm.facecloud.ciuser.dao.CitextDao;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Citext;

public class CitextDaoImpl implements CitextDao {

	JdbcTemplate jdbcTemplate;
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	//######################################################################//
	//1 增
	@Override
	public int addCitext(Citext Citext) {
		//text who level createtime labid
		String sql = "INSERT INTO tb_Citext VALUES(null, ?,?,?, NOW(), ?)";
		
		try {
			int i = jdbcTemplate.update(sql, new Object[] {
					Citext.getText(),
					Citext.getWho(),
					Citext.getLevel(),
					Citext.getLabid()
			});
			
			return i;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		
	}
	
	//######################################################################//
	//2 删
	/**
	 * 按照记录id删除
	 * 
	 * */
	@Override
	public int deleteCitext(int id) {
		String sqlString = "DELETE FROM tb_citext WHERE id = ?";
		
		int i = jdbcTemplate.update(sqlString, new Object[] {
				id
		});
		
		return i;
	}
	//######################################################################//
	//3 改
	
	@Override
	public int updateCitext(Citext Citext) {
		String sql = "UPDATE tb_Citext SET `text`=?, `who`=?, `level`=?, edittime=NOW()  WHERE id= ? ";
		
		int i = jdbcTemplate.update(sql, new Object[] {
				Citext.getText(),
				Citext.getWho(),
				Citext.getLevel(),
				Citext.getId()
		});
		
		return i;
	}
	//######################################################################//
	//4 查询
	
	/**
	 * 按照实验室id查询
	 * */
	@Override
	public List<Citext> findCitextByLabid(int labid) {
		
		String sqlString = "SELECT * FROM tb_citext WHERE labid=? ORDER BY level DESC";
		
		try {
			List<Citext> r = jdbcTemplate.query(sqlString, new BeanPropertyRowMapper<>(Citext.class), new Object[] {
					labid
			});
			return r;
		} catch (Exception e) {
			System.out.println(e.getClass());
			return null;
		}
	}
	
	/**
	 * 按照记录id查询
	 * */
	@Override
	public List<Citext> findCitextByid(int id) {
		
		String sqlString = "SELECT * FROM tb_citext WHERE id=?";
		
		try {
			List<Citext> r = jdbcTemplate.query(sqlString, new BeanPropertyRowMapper<>(Citext.class), new Object[] {
					id
			});
			return r;
		} catch (Exception e) {
			System.out.println(e.getClass());
			return null;
		}
	}

}
